package com.resou.reader.bookstore.freeandfinish;

import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.FinishedBookBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.BasePresenter;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.base.m.IModel;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.mine.IView.UserInstance;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * 完结页面P层
 */
public class FinishBookPresenter extends ResouBasePresenter<FinishBookContract.IFinishBookView> implements FinishBookContract.IFinishBookPresenter {

    private StoreService mService;
    private int pageTotal = 0;

    public FinishBookPresenter(FinishBookContract.IFinishBookView view) {
        super(view);
        mService = ApiImp.getInstance().getService(StoreService.class);
    }

    /**
     * 查询完结页接口
     *
     * @param page
     */
    @Override
    public void quearFinishDataList(int page) {
        Observable<Result<FinishedBookBean>> observable;
        if ("0".equals(mView.getType())) {
            observable = mService.finishedBooks(page, 20, UserInstance.getToken());
        } else {
            observable = mService.freeBooks(page, 20, UserInstance.getToken());
        }
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<FinishedBookBean>>() {
                    @Override
                    public void accept(Result<FinishedBookBean> finishedBookBeanResult) throws Exception {
                        FinishedBookBean finishedBookBean = finishedBookBeanResult.getData();
                        if (finishedBookBean != null) {
                            pageTotal = finishedBookBean.getPage();
                            if ((page + 1) * pageSize < pageTotal) {
                                if (page == 0) {
                                    mView.getSmartRefreshLayout().finishRefresh();
                                    mView.getAdapter().loadMoreEnd();
                                }
                                mView.getAdapter().loadMoreComplete();
                                mView.showDataList(finishedBookBean.getBooks());
                            } else {
                                mView.getAdapter().loadMoreEnd();
                                mView.showDataList(finishedBookBean.getBooks());
                            }
                            mView.showContent();
                        } else {
//                            DLog.d(FinishBookPresenter.class.getSimpleName(), "finishedBookBean == null!");
                            mView.showEmpty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showContent();
                        throwable.printStackTrace();
                        mView.showError(throwable);
                    }
                });
    }
}
